package com.itheima.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Comments;
import com.itheima.mapper.CommentsMapper;
import com.itheima.pojo.po.CommentsPo;
import com.itheima.pojo.po.UserPo;
import com.itheima.result.PageResult;
import com.itheima.service.CommentsService;
import com.itheima.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-15
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private CommentsService commentsService;

    @GetMapping("/projectId")
    @ApiOperation("根据项目Id查询评论列表")
    public Result commentsProject(Integer id) {
        if (id == null) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        List<CommentsPo> comments = commentsService.selectByProjectId(id);
        return Result.success(comments);
    }

    @PostMapping("/list")
    @ApiOperation("评论列表")
    public Result<PageResult>  commentsUserIdList(@RequestBody CommentsPo comment) {
        PageResult pageResult = commentsService.selectByUserId(comment);
        return Result.success(pageResult);
    }

    @PostMapping("/contentsAdd")
    @ApiOperation("评论新增")
    public Result contentsAdd(@RequestBody Comments comments) {
        if (comments.getBuserId() == null || comments.getProjectId() == null
                || comments.getContent() == null || comments.getUserId() == null) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        commentsService.contentsAdd(comments);
        return Result.success();
    }
    @PostMapping("/commentSupport")
    @ApiOperation("评论点赞")
    public Result commentSupport(@RequestBody Comments comments) {
        if (comments.getId() == null || comments.getSupport() == null) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        commentsService.commentSupport(comments);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("评论删除")
    public Result commentDelete(@RequestParam("ids") Integer[] ids) {
        if (ids.length == 0) {
            return Result.error(MessageConstant.ACCOUNT_CHANSHU);
        }
        commentsService.commentDelete(ids);
        return Result.success();
    }
    @PostMapping("/getMemberInfo")
    @ApiOperation("根据项目Id查询评论列表")
    public Result getMemberInfo(@RequestBody UserPo userPo) {
        List<UserPo> userPos = commentsService.getMemberInfo(userPo.getUsernames());
        return Result.success(userPos);
    }

}
